package com.htjc.integral.interaction.inner.controller;

import com.htjc.integral.interaction.inner.service.InvokeService;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;

@Slf4j(topic="innerLogger")
@RestController
@RequestMapping({"/invokeController"})
public class InvokeController
{

  @Autowired
  InvokeService invokeService;
  
  @RequestMapping(value ="/invokeQuery",method=RequestMethod.POST)
  @ResponseBody
  public String invokeQuery(@RequestBody String req)
  {
	  log.info("\n\n\n "+InvokeController.class.getName()+"方法名：invokeQuery()渠道端内网应用积分e+内网服务接口=====start=====");
	  String proviceCode = null;
	  String wsdlUrl = null;
	  String targetNameSpace = null;
	  String method = null;
	  String serPath = null;
	  String serMethodName = null;
	  String userName = null;
	  String passWord = null;
	  String result = null;
	  
	  log.info("方法：invokeQuery()++++++++++接收的请求参数req:"+req);
	  try {
		  if(req == null || req.equals("")){
			  String str = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><RESEMES><RETURN_MSG>请求报文为空</RETURN_MSG></RESEMES>";
			  log.info(proviceCode+"||积分e+用户查询报文："+str);
			  return str;
		  }
		  
		 Document doc = DocumentHelper.parseText(req);
		 //获取xml文件的根节点
		 Element rootElement = doc.getRootElement();
		 
		 Element proviceCodeEl = rootElement.element("PROVINCE_CODE");
		 Element wsdlUrlEl = rootElement.element("WSDL_URL");
		 Element targetNameSpaceEl = rootElement.element("TARGET_NAME_SPACE");
		 Element methodEl = rootElement.element("METHOD");
		 Element serPathEl = rootElement.element("SER_PATH");
		 Element serMethodNameEl = rootElement.element("SER_METHOD_NAME");
		 Element userNameEl = rootElement.element("USER_NAME");
		 Element passWordEl = rootElement.element("PASS_WORD");
		 Element dataEl = rootElement.element("DATA");
		 
		 if(proviceCodeEl==null) {
			 String str = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><RESEMES><RETURN_MSG>网省编号为空</RETURN_MSG></RESEMES>";
			 return str;
		 }
		 
		 if(wsdlUrlEl==null) {
			 String str = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><RESEMES><RETURN_MSG>网省地址url为空</RETURN_MSG></RESEMES>";
			 return str;
		 }
		 
		 if(targetNameSpaceEl==null) {
			 String str = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><RESEMES><RETURN_MSG>网省命名空间为空</RETURN_MSG></RESEMES>";
			 return str;
		 }
		 
		 if(methodEl==null) {
			 String str = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><RESEMES><RETURN_MSG>网省方法名称为空</RETURN_MSG></RESEMES>";
			 return str;
		 }
		 
		 if(serPathEl==null) {
			 String str = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><RESEMES><RETURN_MSG>网省服务地址为空</RETURN_MSG></RESEMES>";
			 return str;
		 }
		 
		 if(serMethodNameEl==null) {
			 String str = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><RESEMES><RETURN_MSG>网省服务方法名称为空</RETURN_MSG></RESEMES>";
			 return str;
		 }
		 
		 if(userNameEl==null) {
			 String str = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><RESEMES><RETURN_MSG>网省账号为空</RETURN_MSG></RESEMES>";
			 return str;
		 }
		 
		 if(passWordEl==null) {
			 String str = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><RESEMES><RETURN_MSG>网省密码为空</RETURN_MSG></RESEMES>";
			 return str;
		 }
		 
		 proviceCode = proviceCodeEl.getText();
		 wsdlUrl = wsdlUrlEl.getText();
		 targetNameSpace = targetNameSpaceEl.getText();
		 method = methodEl.getText();
		 serPath = serPathEl.getText();
		 serMethodName = serMethodNameEl.getText();
		 userName = userNameEl.getText();
		 passWord = passWordEl.getText();
		  
		 Object object = invokeService.invokeQuery(dataEl.getText(),proviceCode,wsdlUrl,targetNameSpace,method,serPath,serMethodName,userName,passWord);
		 
		 result = object != null ? object.toString() : null;
		 log.info("返回值:"+object);
		  
		 log.info(InvokeController.class.getName()+"方法名：invokeQuery()渠道端内网应用积分e+内网服务接口=====end=====");
	  } catch (DocumentException e) {
		  // TODO Auto-generated catch block
		  e.printStackTrace();
	  }
	  return result;
  }
}